# must be nonempty
PKGID=flash.utils
PKGPATH=flash/utils
CLASS=FastMath

all : $(CLASS).swc

clean :
	rm -rf skeleton *.swf *.swc *.abc *.xml *achacks* *.pp

skeleton/$(PKGPATH)/$(CLASS).as :
	mkdir -p skeleton/$(PKGPATH)
	echo 'package $(PKGID) { public class $(CLASS) {} }' > "$@"

skeleton/$(CLASS).swc : skeleton/$(PKGPATH)/$(CLASS).as
	compc -target-player 10.0.0 "$(PKGPATH)/$(CLASS)" -output "$@" -source-path skeleton

skeleton/$(CLASS).swf : skeleton/$(CLASS).swc
	unzip "$<" library.swf
	echo -n 'F' > "$@"
	dd if=library.swf bs=1 count=7 seek=1 skip=1 of="$@"
	if type -p zpipe; then ZPIPE=zpipe; else ZPIPE=zpipe.pl; fi ;\
	dd if=library.swf bs=8 skip=1 | $$ZPIPE -d >> "$@"
	rm library.swf

# checks for cygpath for cygwin
%.abc : %.pp.as
	if type cygpath >/dev/null 2>&1; then ALCHEMY_HOME=$$(cygpath -m $${ALCHEMY_HOME}); fi; \
java -Xms16M -jar "$$ASC" -AS3 -strict -import "$${ALCHEMY_HOME}/flashlibs/playerglobal.abc" -import "$${ALCHEMY_HOME}/flashlibs/global.abc" "$<"
	mv "$*.pp.abc" "$*.abc"
	@[[ -s $@ ]]

%.pp.as: %.as
	cpp -P "$<" > "$@"
	@[[ -s $@ ]]

%.swf : %.abc skeleton/%.swf
	PutABC2.pl skeleton/"$*".swf $@ "$*".abc $(PKGPATH)/$(CLASS)
	@[[ -s $@ ]]

%.swc : %.swf %.xml
	cp $*.xml catalog.xml
	cp $*.swf library.swf
	zip -m $@ catalog.xml library.swf

%.xml : %.as
	echo \
'<swc xmlns="http://www.adobe.com/flash/swccatalog/9">'       \
'  <versions>'                                                \
'    <swc version="1.0" />'                                   \
'    <flex version="2.0" build="143452" />'                   \
'  </versions>'                                               \
'  <features>'                                                \
'    <feature-script-deps />'                                 \
'    <feature-files />'                                       \
'  </features>'                                               \
'  <libraries>'                                               \
'    <library path="library.swf">'                            \
'      <script name="'$(PKGPATH)/$*'"  mod="1177909560000" >' \
'        <def id="'$(PKGID):$*'" />'                          \
'        <dep id="AS3" type="n" />'                           \
'        <dep id="Object" type="i" />'                        \
'      </script>'                                             \
'    </library>'                                              \
'  </libraries>'                                              \
'  <files>'                                                   \
'  </files>'                                                  \
'</swc>' > $@
